package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.SettingBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class TouchFeedbackActivity extends AppActivity {

    private TextView tvFollowSystem;
    private SwitchButton switchFollowSystem;
    private TextView tvVibrate;
    private SwitchButton switchVibrate;
    private TextView tvSoundEffect;
    private SwitchButton switchSoundEffect;
    private boolean isFollowSystem;
    private boolean isShock;
    private boolean isVoice;
    private SettingBean.SettingDTO.TouchFeedbackDTO data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_touch_feedback;
    }

    @Override
    protected void initView() {
        isFollowSystem = getIntent().getBooleanExtra("isFollowSystem", false);
        isShock =  getIntent().getBooleanExtra("isShock", false);
        isVoice =  getIntent().getBooleanExtra("isVoice", false);
        tvFollowSystem = findViewById(R.id.tv_follow_system);
        switchFollowSystem = findViewById(R.id.switch_follow_system);
        tvVibrate = findViewById(R.id.tv_vibrate);
        switchVibrate = findViewById(R.id.switch_vibrate);
        tvSoundEffect = findViewById(R.id.tv_sound_effect);
        switchSoundEffect = findViewById(R.id.switch_sound_effect);
        setOnClickListener(R.id.switch_follow_system, R.id.switch_vibrate, R.id.switch_sound_effect);
        updateView();
    }

    private void updateView(){
        if(isFollowSystem){
            switchVibrate.setEnabled(false);
            switchSoundEffect.setEnabled(false);
            switchVibrate.setChecked(false);
            switchSoundEffect.setChecked(false);
        }else{
            switchVibrate.setChecked(isShock);
            switchSoundEffect.setChecked(isVoice);
        }
        switchFollowSystem.setChecked(isFollowSystem);
        tvFollowSystem.setSelected(switchFollowSystem.isChecked());
        tvVibrate.setSelected(switchVibrate.isChecked());
        tvSoundEffect.setSelected(switchSoundEffect.isChecked());
    }

    @Override
    protected void initData() {
        LLHttpManager.getTouchFeedbackSetting(this, new HttpCallback<HttpData<SettingBean.SettingDTO.TouchFeedbackDTO>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.common_loading));
            }

            @Override
            public void onSucceed(HttpData<SettingBean.SettingDTO.TouchFeedbackDTO> result) {
                data = result.getData();
                isFollowSystem = data.isFollowSystem();
                isShock =  data.isShock();
                isVoice =  data.isVoice();
                updateView();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void setting() {
        LLHttpManager.setTouchFeedbackSetting(this, data, new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.setting));
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                toast(R.string.set_success);
                updateView();
                Intent intent = new Intent();
                intent.putExtra("isFollowSystem", isFollowSystem);
                intent.putExtra("isShock", isShock);
                intent.putExtra("isVoice", isVoice);
                setResult(RESULT_OK, intent);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.switch_follow_system:
                isFollowSystem = switchFollowSystem.isChecked();
                data.setFollowSystem(isFollowSystem);
                switchFollowSystem.setChecked(!isFollowSystem);
                setting();
                break;
            case R.id.switch_vibrate:
                isShock = switchVibrate.isChecked();
                data.setShock(isShock);
                switchVibrate.setChecked(!isShock);
                setting();
                break;
            case R.id.switch_sound_effect:
                isVoice = switchSoundEffect.isChecked();
                data.setVoice(isVoice);
                switchSoundEffect.setChecked(!isVoice);
                setting();
                break;
        }
    }
}